<?php

namespace Home\Controller;

class MyrefundController extends BaseMemberController {

    function _initialize() {
        parent::_initialize();
        $this->_user_id = session('visitor.user_id');
        $this->_user_name = session('visitor.user_name');
        $this->_refund_mod = M('refund');
        $this->_refund_message_mod = M('refund_message');
        $this->_order_mod = M('order');
        $this->_ordergoods_mod = M('order_goods');
        $this->_order_extm_mod = M('order_extm');
    }

    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=get_refund_list 获取退款列表
     * @apiName 获取退款列表
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取退款列表
     * 
     * @apiSuccess {String} refund_id 退货ID
     * @apiSuccess {String} refund_sn 退货编号
     * @apiSuccess {String} order_id 订单ID
     * @apiSuccess {String} goods_id 商品ID
     * @apiSuccess {String} spec_id 规格ID
     * @apiSuccess {String} refund_reason 退货原因
     * @apiSuccess {String} refund_desc 退货描述
     * @apiSuccess {String} total_fee 总金额
     * @apiSuccess {String} goods_fee 商品金额
     * @apiSuccess {String} shipping_fee 物流费
     * @apiSuccess {String} refund_goods_fee 退款中商品所退金额
     * @apiSuccess {String} refund_shipping_fee 退款中运费所退金额
     * @apiSuccess {String} buyer_id 买家ID
     * @apiSuccess {String} seller_id 卖家ID
     * @apiSuccess {String} status 状态
     * @apiSuccess {String} refund_shipped 收货情况
     * @apiSuccess {String} ask_customer 客服介入
     * @apiSuccess {String} created 提交时间
     * @apiSuccess {String} end_time 结束时间
     * @apiSuccess {String} store_name 店铺名称
     * @apiSuccess {String} order_sn 订单号
     * 
     */
    function get_refund_list() {
        $map['refund.buyer_id'] = $this->_user_id;
        $refund_list = $this->_refund_mod
                ->field('refund.*,s.store_name,s.owner_name,g.goods_name,o.order_sn')
                ->alias('refund')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store s ON s.store_id = refund.seller_id')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'goods g ON g.goods_id = refund.goods_id')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'order o ON o.order_id = refund.order_id')
                ->where($map)
                ->page(I('request.page', 1, 'intval') . ',10')
                ->order('created desc')
                ->select();
        if ($refund_list) {
            foreach ($refund_list as $key => $refund) {
                $refund_list[$key]['created'] = date('Y-m-d H:i:s', $refund['created']);
                $refund_list[$key]['end_time'] = date('Y-m-d H:i:s', $refund['end_time']);
                $refund_list[$key]['status_name'] = $this->_get_status_name($refund['status']);
                $refund_list[$key]['refund_total_fee'] = $refund['refund_goods_fee'] + $refund['refund_shipping_fee'];
            }
        }
        render('10000', '', array('refund_list' => $refund_list));
    }

    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=refund_add_get 新增退款商品显示
     * @apiName 新增退款商品显示
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 新增退款商品显示
     * 
     * @apiParam {String} order_id 订单ID
     * @apiParam {String} goods_id 商品ID
     * @apiParam {String} spec_id  规格ID
     * 
     */
    function refund_add_get() {
        $order_id = I('request.order_id', 0, 'intval');
        $goods_id = I('request.goods_id', 0, 'intval');
        $spec_id = I('request.spec_id', 0, 'intval');

        if (empty($order_id) || empty($goods_id) || empty($spec_id)) {
            render('19999', '参数错误');
        }

        $order = $this->_order_mod
                ->where('buyer_id=' . $this->_user_id . ' AND order_id=' . $order_id)
                ->find();
        if (empty($order)) {
            render('19999', '订单不存在');
        }
        if ($order['status'] != ORDER_ACCEPTED && $order['status'] != ORDER_SHIPPED && $order['status'] != ORDER_FINISHED) {
            render('19999', '订单当前状态不允许申请退款');
        }
        if ($order['payment_code'] == 'cod' || $order['payment_code'] == 'bank' || $order['payment_code'] == 'post') {
            render('19999', '当前支付方式不允许退款');
        }
        //获取单个商品规格
        $goods = $this->_ordergoods_mod->where('order_id=' . $order_id . ' AND goods_id=' . $goods_id . ' AND spec_id=' . $spec_id)->find();
        if (empty($goods)) {
            render('19999', '订单商品不存在');
        }
        if ($this->_refund_mod->where('order_id=' . $order_id . ' AND goods_id=' . $goods_id . ' AND spec_id=' . $spec_id)->find()) {
            render('19999', '已经申请了退款');
        }
        $order_extm = $this->_order_extm_mod->where('order_id=' . $order_id)->find();
        if (empty($order_extm)) {
            render('19999', '订单商品不存在');
        }

        //优惠后的价格
        $goods_amount_after_adjust = $order['goods_amount'];
        //未优惠的价格
        $goods_amount_before_adjust = $this->_get_total_amount($order_id);

        if ($goods_amount_before_adjust > 0) {
            $goods_share_rate = round($goods['price'] * $goods['quantity'] / $goods_amount_before_adjust, 2);
        } else {
            $goods_share_rate = 0;
        }

        if ($goods_amount_before_adjust == $goods_amount_after_adjust) {
            //最多申请退的商品费用
            $goods['goods_fee'] = round($goods['price'] * $goods['quantity'] - $order['discount'] * $goods_share_rate, 2);
        } elseif ($goods_amount_before_adjust != 0) {
            $goods_total_fee = $goods['price'] * (1 - ($goods_amount_before_adjust - $goods_amount_after_adjust) / $goods_amount_before_adjust) * $goods['quantity'];
            //最多申请退的商品费用
            $goods['goods_fee'] = round($goods_total_fee, 2);
        } else {
            //最多申请退的商品费用
            $goods['goods_fee'] = 0;
        }

        $ordergoods = $this->_ordergoods_mod->where('order_id=' . $order_id)->select();
        if ($goods_amount_before_adjust > 0) {
            //最多申请退的运费
            $goods['shipping_fee'] = round($order_extm['shipping_fee'] * $goods_share_rate, 2);
        } else {
            //最多申请退的运费
            $goods['shipping_fee'] = round($order_extm['shipping_fee'] / count($ordergoods), 2); // 平均分
        }
        //由此区分  以上共用
        $refund = array();
        $refund['goods_fee'] = $refund['refund_fee'] = $goods['goods_fee'];
        $refund['total_fee'] = $goods['goods_fee'] + $goods['shipping_fee'];
        $refund['shipping_fee'] = $goods['shipping_fee'];
        render('10000', '', array('refund' => $refund));
    }

    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=refund_add_post 新增退款商品提交
     * @apiName 新增退款商品的具体信息
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 新增退款商品的具体信息
     * 
     * @apiParam {String} order_id 订单ID
     * @apiParam {String} goods_id 商品ID
     * @apiParam {String} spec_id  规格ID
     * 
     */
    function refund_add_post() {
//        $this->test();
        $order_id = I('request.order_id', 0, 'intval');
        $goods_id = I('request.goods_id', 0, 'intval');
        $spec_id = I('request.spec_id', 0, 'intval');

        if (empty($order_id) || empty($goods_id) || empty($spec_id)) {
            render('19999', '参数错误');
        }

        $order = $this->_order_mod
                ->where('buyer_id=' . $this->_user_id . ' AND order_id=' . $order_id)
                ->find();
        if (empty($order)) {
            render('19999', '订单不存在');
        }
        if ($order['status'] != ORDER_ACCEPTED && $order['status'] != ORDER_SHIPPED && $order['status'] != ORDER_FINISHED) {
            render('19999', '订单当前状态不允许申请退款');
        }
        if ($order['payment_code'] == 'cod' || $order['payment_code'] == 'bank' || $order['payment_code'] == 'post') {
            render('19999', '当前支付方式不允许退款');
        }
        //获取单个商品规格
        $goods = $this->_ordergoods_mod->where('order_id=' . $order_id . ' AND goods_id=' . $goods_id . ' AND spec_id=' . $spec_id)->find();
        if (empty($goods)) {
            render('19999', '订单商品不存在');
        }
        if ($this->_refund_mod->where('order_id=' . $order_id . ' AND goods_id=' . $goods_id . ' AND spec_id=' . $spec_id)->find()) {
            render('19999', '已经申请了退款');
        }
        $order_extm = $this->_order_extm_mod->where('order_id=' . $order_id)->find();
        if (empty($order_extm)) {
            render('19999', '订单商品不存在');
        }

        //优惠后的价格
        $goods_amount_after_adjust = $order['goods_amount'];
        //未优惠的价格
        $goods_amount_before_adjust = $this->_get_total_amount($order_id);

        if ($goods_amount_before_adjust > 0) {
            $goods_share_rate = round($goods['price'] * $goods['quantity'] / $goods_amount_before_adjust, 2);
        } else {
            $goods_share_rate = 0;
        }

        if ($goods_amount_before_adjust == $goods_amount_after_adjust) {
            //最多申请退的商品费用
            $goods['goods_fee'] = round($goods['price'] * $goods['quantity'] - $order['discount'] * $goods_share_rate, 2);
        } elseif ($goods_amount_before_adjust != 0) {
            $goods_total_fee = $goods['price'] * (1 - ($goods_amount_before_adjust - $goods_amount_after_adjust) / $goods_amount_before_adjust) * $goods['quantity'];
            //最多申请退的商品费用
            $goods['goods_fee'] = round($goods_total_fee, 2);
        } else {
            //最多申请退的商品费用
            $goods['goods_fee'] = 0;
        }

        $ordergoods = $this->_ordergoods_mod->where('order_id=' . $order_id)->select();
        if ($goods_amount_before_adjust > 0) {
            //最多申请退的运费
            $goods['shipping_fee'] = round($order_extm['shipping_fee'] * $goods_share_rate, 2);
        } else {
            //最多申请退的运费
            $goods['shipping_fee'] = round($order_extm['shipping_fee'] / count($ordergoods), 2); // 平均分
        }

        //由此区分  以上共用
        $this->_check_post_data($goods);

        $refund_shipping_fee = I('post.refund_shipping_fee', 0);
        $refund_shipped = I('post.refund_shipped');
        $refund_data = array(
            'refund_sn' => $this->_gen_refund_sn(),
            'order_id' => $order_id,
            'goods_id' => $goods_id,
            'spec_id' => $spec_id,
            'refund_reason' => I('post.refund_reason'),
            'refund_desc' => I('post.refund_desc'),
            'total_fee' => $goods['goods_fee'] + $goods['shipping_fee'],
            'goods_fee' => $goods['goods_fee'],
            'shipping_fee' => $goods['shipping_fee'],
            'refund_goods_fee' => I('post.refund_goods_fee'),
            'refund_shipping_fee' => $refund_shipping_fee,
            'refund_shipped' => $refund_shipped,
            'buyer_id' => $order['buyer_id'],
            'seller_id' => $order['seller_id'],
            'status' => 'WAIT_SELLER_AGREE', // 买家已经申请退款，等待卖家同意
            'created' => time(),
        );
        $refund_id = $this->_refund_mod->add($refund_data);
        if ($refund_id) {
            $this->_refund_message_mod->add(array(
                'owner_id' => $this->_user_id,
                'owner_role' => 'buyer',
                'refund_id' => $refund_id,
                'content' => sprintf('买家申请了退款，退款金额为：%s元，退运费金额：%s元，收货情况为：%s，退款原因为：%s，退款说明为：%s', I('post.refund_goods_fee'), $refund_shipping_fee, $refund_shipped, I('post.refund_reason'), I('post.refund_desc')),
                'created' => time(),
            ));
            M('order_log')->add(array(
                'order_id' => $order_id,
                'operator' => $this->_user_name,
                'order_status' => '',
                'changed_status' => '退款申请',
                'remark' => '买家申请退款',
                'log_time' => time(),
                'operator_type' => 'buyer'
            ));
            render('10000', '新增成功');
        } else {
            render('19999', '新增失败');
        }
    }

    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=refund_edit 获取退款商品的具体信息
     * @apiName 获取退款商品的具体信息
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取退款商品的具体信息
     * 
     * @apiParam {String} refund_id 日志ID
     * 
     * @apiParam {String} refund_goods_fee  退款商品费用
     * @apiParam {String} refund_shipping_fee 退款物流费用
     * @apiParam {String} refund_shipped 收货情况
     * @apiParam {String} refund_reason 退款原因
     * @apiParam {String} refund_desc 退款备注
     * 
     */
    function refund_edit() {
        if (IS_POST) {
            $refund_id = I('request.refund_id', 0, 'intval');
            if (empty($refund_id)) {
                render('19999', '参数错误');
            }
            $refund = $this->_refund_mod->where('(status != "SUCCESS" AND status != "CLOSED" AND status != "WAIT_ADMIN_AGREE") and refund_id=' . $refund_id . ' and buyer_id=' . $this->_user_id)->find();
            if (!$refund) {
                render('19999', '退款不可编辑');
                return;
            }
            $this->_check_post_data($refund);

            $refund_shipped = I('post.refund_shipped');
            $refund_shipping_fee = I('post.refund_shipping_fee', 0);
            $data = array(
                'status' => 'WAIT_SELLER_CONFIRM',
                'refund_reason' => I('post.refund_reason'),
                'refund_desc' => I('post.refund_desc'),
                'refund_goods_fee' => I('post.refund_goods_fee'),
                'refund_shipping_fee' => $refund_shipping_fee,
                'refund_shipped' => $refund_shipped,
            );
            $this->_refund_mod->where('refund_id=' . $refund_id)->save($data);
            $this->_refund_message_mod->add(array(
                'owner_id' => $this->_user_id,
                'owner_role' => 'buyer',
                'refund_id' => $refund_id,
                'content' => sprintf('买家修改了退款申请，退款金额修改为：%s元，退运费金额：%s元，收货情况修改为：%s，退款原因修改为：%s，退款说明修改为：%s', I('post.refund_goods_fee'), $refund_shipping_fee, $refund_shipped, I('post.refund_reason'), I('post.refund_desc')),
                'created' => time(),
            ));
            render('10000', '修改退款成功');
        }
    }

    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=refund_cancel 取消退款
     * @apiName 取消退款
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 取消退款
     * 
     * @apiParam {String} refund_id 日志ID
     * 
     */
    function refund_cancel() {
        $refund_id = I('request.refund_id', 0, 'intval');
        if (empty($refund_id)) {
            render('19999', '参数错误');
        }
        $refund = $this->_refund_mod->where('(status != "SUCCESS" AND status != "CLOSED" AND status != "WAIT_ADMIN_AGREE") and refund_id=' . $refund_id . ' and buyer_id=' . $this->_user_id)->find();
        if (!$refund) {
            render('19999', '退款不可编辑');
            return;
        }
        $this->_refund_mod->where('refund_id=' . $refund_id)->save(array('status' => 'CLOSED'));

        $this->_refund_message_mod->add(array(
            'owner_id' => $this->_user_id,
            'owner_role' => 'buyer',
            'refund_id' => $refund_id,
            'content' => sprintf('取消退款，退款编号为：%s', $refund['refund_sn']),
            'created' => time(),
        ));
        render('10000', '取消退款成功');
    }

    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=refund_ask_customer 要求客服介入处理
     * @apiName 要求客服介入处理
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 要求客服介入处理
     * 
     * @apiParam {String} refund_id 日志ID
     * 
     */
    function refund_ask_customer() {
        $refund_id = I('request.refund_id', 0, 'intval');
        if (empty($refund_id)) {
            render('19999', '参数错误');
        }
        $refund = $this->_refund_mod->where('(status != "SUCCESS" AND status != "CLOSED" AND status != "WAIT_ADMIN_AGREE") and refund_id=' . $refund_id . ' and buyer_id=' . $this->_user_id)->find();
        if (!$refund) {
            render('19999', '退款不可编辑');
            return;
        }
        
        if ($refund['ask_customer']==1) {
            render('19999', '客服已申请介入处理');
            return;
        }
        
        $this->_refund_mod->where('refund_id=' . $refund_id)->save(array('ask_customer' => '1'));

        if ($refund['buyer_id'] == $this->_user_id) {
            $owner_role = 'buyer';
        } else {
            $owner_role = 'seller';
        }

        $this->_refund_message_mod->add(array(
            'owner_id' => $this->_user_id,
            'owner_role' => $owner_role,
            'refund_id' => $refund_id,
            'content' => sprintf('%s要求客服介入处理', $this->_user_name),
            'created' => time(),
        ));
        render('10000', '客服介入处理');
    }

    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=get_refund_detail 获取退款详情
     * @apiName 获取退款详情
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取退款详情
     * 
     * @apiParam {String} refund_id 日志ID
     * 
     */
    function get_refund_detail() {
        $refund_id = I('request.refund_id', 0, 'intval');
        if (empty($refund_id)) {
            render('19999', '参数错误');
        }
        $map['refund_id'] = $refund_id;
        $map['refund.buyer_id'] = $this->_user_id;
        $refund = $this->_refund_mod
                ->field('refund.*,s.store_name,s.store_id,s.owner_name,g.goods_name,o.order_sn')
                ->alias('refund')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store s ON s.store_id = refund.seller_id')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'goods g ON g.goods_id = refund.goods_id')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'order o ON o.order_id = refund.order_id')
                ->where($map)
                ->find();
        if (empty($refund)) {
            render('19999', '退款不存在');
        }
        $refund['refund_total_fee'] = $refund['refund_goods_fee'] + $refund['refund_shipping_fee'];
        $refund['status_name'] = $this->_get_status_name($refund['status']);
        if ($refund['status'] != 'SUCCESS' && $refund['status'] != 'CLOSED' && $refund['status'] != 'FAILED' && $refund['status'] != 'WAIT_ADMIN_AGREE') {
            $refund['button']['cancel_status'] = 1; //是否显示取消
            $refund['button']['edit_status'] = 1; //是否显示编辑
            if ($refund['ask_customer'] != 1) {
                $refund['button']['ask_status'] = 1; //是否显示客服是否介入
            }
        } else {
            $refund['button']['cancel_status'] = 0;
            $refund['button']['edit_status'] = 0;
            $refund['button']['ask_status'] = 0;
        }
        
        //格式化时间
        $refund['created']=date('Y-m-d H:i:s',$refund['created']);
        
        render('10000', '', array('refund' => $refund));
    }
    
    
    private function _get_status_name($status){
        switch ($status) {
            case 'WAIT_SELLER_AGREE':
                $status_name = '等待卖家同意';
                break;
            case 'SELLER_REFUSE_BUYER':
                $status_name = '等待买家修改';
                break;
            case 'WAIT_SELLER_CONFIRM':
                $status_name = '等待卖家确定';
                break;
            case 'WAIT_ADMIN_AGREE':
                $status_name = '等待管理员同意';
                break;
            case 'SUCCESS':
                $status_name = '退款成功';
                break;
            case 'CLOSED':
                $status_name = '退款关闭';
                break;
            case 'FAILED':
                $status_name = '退款失败';
                break;
            default:
                break;
        }
        return $status_name;
    }

    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=get_refund_message 获取退款协商详情
     * @apiName 获取退款协商详情
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取退款协商详情
     * 
     * @apiSuccess {String} rm_id 协商ID
     * @apiSuccess {String} owner_id 用户ID
     * @apiSuccess {String} owner_role 角色名称
     * @apiSuccess {String} refund_id 退款ID
     * @apiSuccess {String} content 协商内容
     * @apiSuccess {String} created 创建时间
     * 
     */
    function get_refund_message() {
        $refund_id = I('request.refund_id', 0, 'intval');
        if (empty($refund_id)) {
            render('19999', '参数错误');
        }
        $map['refund_id'] = $refund_id;
        $map['owner_id'] = $this->_user_id;
        $refund_message_list = $this->_refund_message_mod->where($map)
                ->page(I('request.page', 1, 'intval') . ',10')
                ->order('created desc')
                ->select();
        if($refund_message_list){
            foreach ($refund_message_list as $key => $refund_message) {
                $refund_message_list[$key]['created'] = date('Y-m-d H:i:s',$refund_message['created']);
            }
        }
        render('10000', '', array('refund_message_list' => $refund_message_list));
    }
    
    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=refund_message_add 新增退款协商详情
     * @apiName 新增退款协商详情
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 新增退款协商详情
     * 
     * @apiParam {String} refund_id 日志ID
     * @apiParam {String} content 是否完成
     * 
     */
    function refund_message_add() {
        $refund_id = I('request.refund_id', 0, 'intval');
        if (empty($refund_id)) {
            render('19999', '参数错误');
        }
        $map['refund_id'] = $refund_id;
        $map['user_id'] = $this->_user_id;
        $refund = $this->_refund_mod->where($map)->find();
        if ($refund['status'] == 'SUCCESS' || $refund['status'] == 'CLOSED' || $refund['status'] == 'FAILED') {
            render('19999', '协商已关闭');
        }
        $content = I('request.content');
        if(empty($content)){
            render('19999', '协商内容不能为空');
        }
        $data = array(
            'owner_id' => $this->_user_id,
            'owner_role' => $refund['buyer_id'] == $this->_user_id ? 'buyer' : ($refund['seller_id'] == $this->_user_id ? 'seller' : 'admin'),
            'refund_id' => $refund_id,
            'content' => $content,
            'created' => time()
        );
        $result = $this->_refund_message_mod->add($data);
        if ($result) {
            render('10000', '提交成功');
        } else {
            render('19999', '添加协商失败');
        }
    }

    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=get_shipped_list 获取收货情况列表
     * @apiName 获取收货情况列表
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取收货情况列表
     */
    function get_shipped_list() {
        $data = array('已收到货,需退货退款', '已收到货,不退货只退款', '未收到货,需要退款');
        render('10000', '', array('shipped_list' => $data));
    }

    /**
     * @api {get} /index.php?m=Home&c=Myrefund&a=get_refund_reason 获取退款原因列表
     * @apiName 获取退款原因列表
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取退款原因列表
     */
    function get_refund_reason() {
        $data = array(
            '缺货',
            '未按约定时间发货',
            '收到假货',
            '商品有质量问题',
            '商品错发/漏发',
            '收到的商品破损',
            '收到的商品描述不符',
            '其他'
        );
        render('10000', '', array('refund_reason_list' => $data));
    }

    function test() {
        $_POST['refund_goods_fee'] = '8';
        $_POST['refund_shipping_fee'] = '0.01';
        $_POST['refund_shipped'] = '123';
        $_POST['refund_reason'] = '退款原因';
        $_POST['refund_desc'] = '退款详情';
    }

    private function _check_post_data($refund = array()) {
        $refund_goods_fee = I('post.refund_goods_fee');
        if (empty($refund_goods_fee) || floatval($refund_goods_fee) < 0) {
            render('19999', '退款金额不能为空且必须大于0');
        } elseif (floatval($refund_goods_fee) > $refund['goods_fee']) {
            render('19999', '退款金额不能小于0或者大于商品总额');
        }
        $refund_shipping_fee = I('post.refund_shipping_fee');
        if ($refund_shipping_fee != '' && floatval($refund_shipping_fee) < 0) {
            render('19999', '退运费金额不能小于0');
        }
        if (floatval($refund_shipping_fee) > $refund['shipping_fee']) {
            render('19999', '退运费金额不能大于该商品分摊的运费金额');
        }
        $refund_reason = I('post.refund_reason');
        if (empty($refund_reason)) {
            render('19999', 'select_refund_reason');
        }
    }

    private function _gen_refund_sn() {
        mt_srand((double) microtime() * 1000000);
        $timestamp = time();
        $y = date('Y', $timestamp);
        $z = date('z', $timestamp);
        $refund_sn = $y . str_pad($z, 3, '0', STR_PAD_LEFT) . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $refund = $this->_refund_mod->where('refund_sn=' . $refund_sn)->find();
        if (!$refund) {
            return $refund_sn;
        }
        return $this->_gen_refund_sn();
    }

    /*
     * 获取当前订单的未优惠的商品总价
     */

    private function _get_total_amount($order_id) {
        $ordergoods_list = $this->_ordergoods_mod->where('order_id=' . $order_id)->select();
        foreach ($ordergoods_list as $ordergoods) {
            $total_amount += $ordergoods['price'] * $ordergoods['quantity'];
        }
        return $total_amount;
    }

    function get_refund_goods_list() {
        if(!$_POST['page'] || !$_POST['pagenum']){
            render('10004', '参数错误');
        }
        $map['refund.buyer_id'] = $this->_user_id;
        $refund_list = $this->_refund_mod
            ->field('refund.*,s.store_name,s.owner_name,g.goods_name,g.default_image,o.order_sn,og.specification,og.price_total,og.quantity,og.goods_image')
            ->alias('refund')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store s ON s.store_id = refund.seller_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'goods g ON g.goods_id = refund.goods_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'order o ON o.order_id = refund.order_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'order_goods og ON og.spec_id = refund.spec_id AND og.order_id = o.order_id')
            ->where($map)
            ->limit($_POST['pagenum'])
            ->page($_POST['page'])
            ->order('created desc')
            ->select();
        if ($refund_list) {
            foreach ($refund_list as $key => $refund) {
                $refund_list[$key]['created'] = date('Y-m-d H:i:s', $refund['created']);
                $refund_list[$key]['end_time'] = date('Y-m-d H:i:s', $refund['end_time']);
                $refund_list[$key]['status_name'] = $this->_get_status_name($refund['status']);
                $refund_list[$key]['refund_total_fee'] = $refund['refund_goods_fee'] + $refund['refund_shipping_fee'];
//                $refund_list[$key]['refund_total_fee']['specification'] ? NULL : $refund_list[$key]['refund_total_fee']['specification'] = '';
//                $refund_list[$key]['refund_total_fee']['price_total'] ? NULL : $refund_list[$key]['refund_total_fee']['specification'] = '0.00';
//                $refund_list[$key]['refund_total_fee']['quantity'] ? NULL : $refund_list[$key]['refund_total_fee']['specification'] = '0';

            }
        }
        render('10000', '', array('refund_list' => $refund_list));
    }

}

?>
